package com.easyj.admin.sys.controller;


import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.easyj.admin.db.model.DirtionaryItem;
import com.easyj.admin.sys.service.DirtionaryService;
import com.easyj.base.common.ajax.Result;
import com.easyj.base.controller.BaseController;
import com.easyj.base.exception.EasyjException;


	/**
	* 管理
	*
	 */
	@Controller
	@RequestMapping(value = "/sys/dirtionary")
	public class DirtionaryController extends BaseController{

		@Autowired
		private  DirtionaryService dirtionaryService;

		
	    /**
	     * 提供给前端取得字典的条目
	     * @param dirtionary
	     * @return
	     */
		@ResponseBody
		@RequestMapping(value = "getDirtionaryItems", method = {RequestMethod.GET})
		public Result getDirtionaryItems(@RequestParam(value = "dirtionary") String dirtionary) {	
			List<DirtionaryItem> item=dirtionaryService.getDirtionaryItems(dirtionary);
			return Result.Success(item);
			
		}
		/**
		 * get all dirtionarys
		 * @return
		 */
		@PreAuthorize("hasPermission('dirtionary','dirtionary:all')")
		@RequestMapping(value = "getAll", method = {RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
	    public Result getAll() {
	        return Result.Success(dirtionaryService.getAllDirtionary());
	    }
		
	    
		@PreAuthorize("hasPermission('dirtionary','dirtionary:all')")
		@RequestMapping(value = "addItem", method = RequestMethod.POST)
		@ResponseBody
		public Result addItem(@RequestBody  @Valid DirtionaryItem entity,BindingResult validResult) throws EasyjException {
					
			if(validResult.hasErrors()){
				return Result.Error("数据不符合要求！");  
			}	
			
			dirtionaryService.addItem(entity);
			return Result.Success(entity);
		}
		
		@PreAuthorize("hasPermission('dirtionary','dirtionary:all')")
		@RequestMapping(value = "editItem", method = RequestMethod.POST)
		@ResponseBody
		public Result editItem(@RequestBody  @Valid DirtionaryItem entity,BindingResult validResult) throws EasyjException {
			if(validResult.hasErrors()){
				return Result.Error("数据不符合要求！");  
			}
			
			this.updateItem(entity);
			return Result.Success("OK");
			
			
		}
		
		/**
		 * 逻辑删除
		 * @param id
		 * @return
		 * @throws EasyjException 
		 */
		@PreAuthorize("hasPermission('dirtionary','dirtionary:all')")
		@RequestMapping(value = "deleteItem", method = RequestMethod.GET)
		@ResponseBody
		public Result deleteItem(@RequestParam(value = "id") Long id) throws EasyjException {
			DirtionaryItem item=dirtionaryService.getItemById(id);
			item.setStatus(0);//
			this.updateItem(item);
			return Result.Success("OK");
			
		}
		
		
		@PreAuthorize("hasPermission('dirtionary','dirtionary:all')")
		@RequestMapping(value = "recoverItem", method = RequestMethod.GET)
		@ResponseBody
		public Result recoverItem(@RequestParam(value = "id") Long id) {
			try{		
				DirtionaryItem item=dirtionaryService.getItemById(id);
				item.setStatus(1);//
				this.updateItem(item);
				return Result.Success("OK");
			}catch(Exception ex){
				return Result.Error( "恢复错误！"+ex.getMessage());
			}
			
		}
		
		/**
		 * 由于要同时更新缓存，使用外部调用evict,变通下
		 * @param item
		 * @throws EasyjException 
		 */
		private void updateItem(DirtionaryItem item) throws EasyjException{
			dirtionaryService.evictDirtionary(item);
			dirtionaryService.evictDirtionaryItem(item);
			dirtionaryService.updateItem(item);
		}
		

		/**
		 * 修改
		* @param model
		* @param id
		* @return
       @PreAuthorize("hasPermission('dirtionary','dirtionary:all')")
		@RequestMapping(value = "editEntity", method = RequestMethod.POST)
		@ResponseBody
		public Result editEntity(@RequestBody @Valid Dirtionary entity,BindingResult validResult) {
			try{
				if(validResult.hasErrors()){
					return Result.Error("数据不符合要求！"); 
				}	
				dirtionaryService.updateEntity(entity);
				return Result.Success("OK");
			}catch(Exception ex){
				return Result.Error( "修改错误！");
			}			
		}
        */
		/**
		* 新增
		* @param model
		* @return
		
        @PreAuthorize("hasPermission('dirtionary','dirtionary:all')")
		@RequestMapping(value = "addEntity", method = RequestMethod.POST)
		@ResponseBody
		public Result addEntity(@RequestBody @Valid Dirtionary entity,BindingResult validResult) {
			try{		
				if(validResult.hasErrors()){
					return Result.Error("数据不符合要求！"); 
				}
				dirtionaryService.insertEntity(entity);
				return Result.Success("OK");
			}catch(Exception ex){
				return Result.Error( "新增错误！");
			}
		}

		*/

}

